﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JOIN.Rights.Model;
using JOIN.Rights.BLL;
using System.Text.RegularExpressions;

namespace JOIN.Rights.Web.Base
{
    public partial class MainForm : System.Web.UI.Page
    {
        List<Kevin.Controls.MenuItem> _MenuList = new List<Kevin.Controls.MenuItem>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["Admin"] == null)
                {
                    return;
                }
                init();
            }
        }

        private void init()
        {
            MenusInfo menu = new MenusInfo() { ProjectID = "rights", MenuParent = "" };

            List<MenusInfo> menus = new MenusBLL(menu).Load(menu.ProjectID, menu.MenuParent, new List<string>() { "100" });
            foreach (MenusInfo menuItem in menus)
            {
                Kevin.Controls.MenuItem item = new Kevin.Controls.MenuItem();
                item.ID = menuItem.MenuID;
                item.Level = menuItem.MenuLevel.ToString();
                item.Link = "";// menuItem.MenuLink;
                item.Text = menuItem.MenuName;
                _MenuList.Add(item);
                findSub(menuItem, ref item);
            }
            this.LeftMenu1.MenuList = _MenuList;
        }

        private void findSub(MenusInfo menu, ref Kevin.Controls.MenuItem menuItem)
        {
            foreach (MenusInfo subItem in menu.SubMenu)
            {
                Kevin.Controls.MenuItem item = new Kevin.Controls.MenuItem();
                item.ID = subItem.MenuID;
                item.Level = subItem.MenuLevel.ToString();
                item.Link = "";// subItem.MenuLink;
                item.Script = string.Format("Goto(\'{0}\',\'{1}\',\'{2}\');", subItem.MenuID, subItem.MenuName, HttpUtility.HtmlEncode( subItem.MenuLink));
                item.Text = subItem.MenuName;
                menuItem.ChildMenu.Add(item);
                findSub(subItem,ref item);
            }
        }
        
        public string Copyright
        {
            get {
                return "";// "权限平台 Copyright @ 苏州捷安信息科技有限公司";
            }
        }

        private string GetRelativePath_Inner(Page page, string filePath)
        {

            string rootPath = page.MapPath("~");//c:\assas

            string currentPath = page.MapPath(filePath);
            if (rootPath.EndsWith("\\") == false)
            {
                rootPath += "\\";
            }
            if (currentPath.EndsWith("\\") == false)
            {
                currentPath += "\\";
            }
            string relativePath;// = currentPath.Substring(rootPath.Length);
            if (currentPath.CompareTo(rootPath) == 0)
            {
                relativePath = "./";
            }
            else
            {
                if (currentPath.StartsWith(rootPath))
                {
                    relativePath = currentPath.Substring(rootPath.Length);
                    relativePath = relativePath.Trim('\\');
                    string[] paths = relativePath.Split('\\');
                    relativePath = "";
                    for (int i = 0; i < paths.Length; i++)
                    {
                        relativePath += "../";
                    }
                }
                else
                {
                    relativePath = "";
                }
            }
            return relativePath;
        }

        public  string GetRelativeURL(Page page, string specifiedPath, string url)
        {
            if (String.IsNullOrEmpty(url))
                return url;
            string pattern = "(http(s){0,1})|(javascript):";
            Regex r = new Regex(pattern, RegexOptions.Multiline);
            Match m = r.Match(url);
            if (m.Success == false || m.Index != 0)
            {
                url = GetRelativePath_Inner(page, specifiedPath) + url;
                return url;
            }
            else
            {
                return url;
            }
        }
    }
}